package com.erp.module.caiLiaoZhiLiang

import android.content.Context
import android.util.Log
import com.erp.bean.CaiLiaoZhiLiangBean
import com.erp.mclass.UnCeHandler
import com.erp.Interface.OnUserDataInterface
import com.erp.util.Httputil
import com.erp.util.JsonUtil
import com.erp.util.Myutil
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject

/**
 * Created by Administrator on 2018/5/14/014.
 * 作者：刘洋
 */
class ICaiLiaoZhiLiangKP(var mlist: MutableList<CaiLiaoZhiLiangBean>, val KView: CaiLiaoZhiLiangContract.CaiLiaoZhiLiangKView, val content: Context) : CaiLiaoZhiLiangContract.CaiLiaoZhiLiangKP {

    val modle : CaiLiaoZhiLiangContract.CaiLiaoZhiLiangMP= CaiLiaoZhiLiangModel()
    var page = 0
    var pagesize = 20
    var sousuo = ""
    var hege = ""
    var startTime =""
    var endTime =""
    //上拉加载
    override fun shangla() {
        UnCeHandler.buzhou = "shangla"
        page++
        httpcailiao(1)
    }

    //下拉刷新
    override fun xiala() {
        UnCeHandler.buzhou = "xiala"
        page = 0
        httpcailiao(2)
    }

    //搜索
    override fun sousuo(sousuo: String,hege : String ,StartTime : String , EndTime : String) {
        UnCeHandler.buzhou = "sousuo"
        this.sousuo = sousuo
        this.hege=hege
        this.startTime=StartTime
        this.endTime=EndTime
        if(startTime==""){
            startTime = "0"
        }else{
            startTime=Myutil.dateToStamp(startTime)
        }
        if(endTime==""){
            endTime = "0"
        }else{
            endTime=Myutil.dateToStamp(endTime)
        }
        page = 0
        httpcailiao(3)
    }

    //删除一条数据
    override fun delete(position : Int) {
        UnCeHandler.buzhou = "delete"
        Httputil.deleteCodeCaiLiao(content, mlist[position].id, object : OnUserDataInterface {
            override fun onUserData(data: String?) {
                KView.deleteOk(position)
            }

            override fun onNot(throwable: String?) {
                KView.deleteNo()
            }

        })

    }

    //网络访问获取数据
    fun httpcailiao(code: Int) {
        UnCeHandler.buzhou = "httpcailiao"
        modle.getGongyingCailiaoZhiliang(content, page, pagesize, sousuo,hege,startTime,endTime,object : OnUserDataInterface {
            override fun onUserData(data: String?) {
                if (data == null) {
                    return
                }
                when (code) {
                    1 -> {
                        getJson(data)
                        KView.shanglaOk()
                    }
                    2 -> {
                        mlist.clear()
                        getJson(data)
                        KView.xialaOk()
                    }
                    3 -> {
                        mlist.clear()
                        getJson(data)
                        KView.sousuoOk()
                    }
                }
            }

            override fun onNot(throwable: String?) {
                Log.d("e", "ex=" + throwable.toString())
                when (code) {
                    1 -> KView.shanglaNo()
                    2 -> KView.xialaNo()
                    3 -> KView.sousuoNo()
                }
            }

        })
    }

    //解析json字符串
    fun getJson(data: String) {
        Log.d("TAAAG", "data=$data---------------")
        UnCeHandler.buzhou = "getJson"
        try {
            val js = JSONArray(data)
            for (i in 0 until js.length()) {
                val jo = js[i] as JSONObject
                var bean = CaiLiaoZhiLiangBean()
                bean.id = JsonUtil.getvolue(jo, "id")
                bean.iu_id = JsonUtil.getvolue(jo, "iu_id")
                bean.gi_id = JsonUtil.getvolue(jo, "gi_id")
                bean.mc_2_id = JsonUtil.getvolue(jo, "mc2_id")
                bean.qi_id = JsonUtil.getvolue(jo, "qi_id")
                bean.jianyan_time = JsonUtil.getvolue(jo, "test_time")
                bean.cailiao_shuliang = JsonUtil.getvolue(jo, "cailiao_shuliang")
                bean.choucha_shuliang = JsonUtil.getvolue(jo, "choucha_shuliang")
                bean.hege_shuliang = JsonUtil.getvolue(jo, "hege_shuliang")
                bean.quality_decision = JsonUtil.getvolue(jo, "quality_decision")
                bean.process_mode = JsonUtil.getvolue(jo, "process_mode")
                bean.solution = JsonUtil.getvolue(jo, "solution")
                bean.iu_name = JsonUtil.getvolue(jo, "iu_name")
                bean.gi_name = JsonUtil.getvolue(jo, "gi_name")
                bean.mc2_name = JsonUtil.getvolue(jo, "mc2_name")
                bean.qi_name = JsonUtil.getvolue(jo, "qi_name")
                bean.description = JsonUtil.getvolue(jo, "description")
                val jsp = JsonUtil.getjsvolue(jo, "files")
                if (jsp != null) {
                    for (j in 0 until jsp.length()) {
                        val jop = jsp.get(j) as JSONObject
                        val photo = CaiLiaoZhiLiangBean.photo()
                        photo.fid = JsonUtil.getvolue(jop, "fid")
                        photo.filename = JsonUtil.getvolue(jop, "filename")
                        photo.uri = JsonUtil.getvolue(jop, "uri")
                        bean.listphoto.add(photo)
                    }
                }
                mlist.add(bean)
            }
        } catch (e: JSONException) {
            e.printStackTrace()
        }
    }
}